<?
require_once("IEndpointRetriever.php");
require_once("UrlEndpointRetriever.php");
class HttpEndpointRetriever implements IEndpointRetriever
{
	protected $urlEndpointRetriever;

	public function HttpEndpointRetriever(UrlEndpointRetriever $urlEndpointRetriever)
	{
		$this->urlEndpointRetriever = $urlEndpointRetriever;
	}

	public function RetrieveEndpoint(Endpoint $endpoint)
	{
		$method = $endpoint->GetMethod();
		$urlData = $this->urlEndpointRetriever->RetrieveEndpoint($endpoint);
		if ($method != "POST")
		{
			return file_get_contents($urlData->Url . "?" . $urlData->QueryString);
		}
		else
		{
			$opts = array(
				'http' => array(
					'method'	=> "POST",
					'header'	=> "Cookie: " . $urlData->Cookie . "\r\n" .
						"Content-type: application/x-www-form-urlencoded\r\n" .
						"Content-Length: " .strlen($urlData->QueryString) . "\r\n",
					'content'	=> ($urlData->QueryString == null ? "" : $urlData->QueryString)
				)
			);
			$context = stream_context_create($opts);
			return file_get_contents($urlData->Url, 0, $context);
		}
	}
}
?>
